package com.example.fasttalk;

import com.example.fasttalk.model.FastTalkGroupModel;
import com.example.fasttalk.client.MessagesManagerClient;
import com.example.fasttalk.setting.FastTalkSettings;
import com.example.fasttalk.window.FastTalkWindowManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowFactory;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import org.jetbrains.annotations.NotNull;

import java.util.List;


public class FastTalkWindowFactory implements ToolWindowFactory {
  @Override
  public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
    // 拿到后台登录账号等相关配置
    FastTalkSettings settings = FastTalkSettings.getInstance();
    // 初始化消息管理 进行登录 建立服务器连接
    MessagesManagerClient instance = MessagesManagerClient.getInstance();
    instance.init(settings.getState());
    // 获取群组列表, 每个群组独占一个 content
    List<FastTalkGroupModel> groupList = instance.getFastTalkGroupList();
    ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();
    for (FastTalkGroupModel fastTalkGroupModel : groupList) {
      FastTalkWindowManager fastTalkWindowManager = new FastTalkWindowManager(fastTalkGroupModel, instance.getCurrentLoginUser());
      Content content = contentFactory.createContent(fastTalkWindowManager.getMainWindow(), fastTalkGroupModel.getName(), false);
      toolWindow.getContentManager().addContent(content);
    }
  }
}

